<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React!</title>
<!-- 先引入react,再引入react-dom,因为react是核心库，react-dom是扩展库 -->
<!-- 引入以后全局多了一个REACT和ReactDOM对象 -->
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<!-- 用于将jsx转为js -->
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
</head>
<style>
    .list {
      width: 200px;
      height: 150px;
      background-color: skyblue;
      overflow: auto;
    }
    .news {
      height: 30px;
    }
</style>
<body>
<!-- 准备好一个容器 -->
<div id="example"></div>
<div id="example1"></div>
<div id="example2"></div>
<!-- 此处一定要写babel，不写就是默认js -->
<script type="text/babel">

class NewsList extends React.Component {

  state = {newsArr:[]}

  componentDidMount(){
    setInterval(() => {
      const {newsArr} = this.state
      const news ='新闻'+(newsArr.length+1)
      this.setState({newsArr:[news,...newsArr]})
    }, 1000);
  }

  getSnapshotBeforeUpdate(){
    // 内容区的高度
    return  this.refs.list.scrollHeight
  }
  componentDidUpdate(props,state,height) {
    this.refs.list.scrollTop += this.refs.list.scrollHeight - height
  }

  render(){
    return (
      <div className="list" ref="list">
        {this.state.newsArr.map((n, index) => {
          return <div className="news" key={index}>{n}</div>
        })}
      </div>
    )
  }
} 





ReactDOM.render(
	<NewsList/>,
	document.getElementById('example')
);

</script>

</body>
</html>